import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/13 9:47
 * @description 二分查找
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] b = {7,7,7,7,12,7,7};
        solution.minDays(b, 2, 3);
    }
    public int minDays(int[] bloomDay, int m, int k) {
        int n = bloomDay.length;
        if (m * k > n) {
            return -1;
        }
        int l = Integer.MAX_VALUE, r = Integer.MIN_VALUE, ans = -1;
        for (int i: bloomDay) {
            if (i < l) {
                l = i;
            }
            if (i > r) {
                r = i;
            }
        }
        while (l <= r) {
            int mid = (l + r) / 2;
            if (check(bloomDay, mid, m, k)) {
                ans = mid;
                r = mid - 1;
            }else {
                l = mid + 1;
            }
        }
        return ans;
    }

    private boolean check(int[] bloomDay, int mid, int m, int k) {
        int cnt = 0;
        for (int day: bloomDay) {
            if (day <= mid) {
                cnt++;
                if (cnt == k) {
                    m--;
                    cnt = 0;
                    if (m == 0) {
                        return true;
                    }
                }
            }else {
                cnt = 0;
            }
        }
        return false;
    }
}
